package com.venusj.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.venusj.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created By Administrator
 * User: VenusZ
 * Date: on 2020/3/23 22:43
 * Desc:
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentInfoGlobalFallbackMethod")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_OK(id);
    }

    @GetMapping(value = "payment/hystrix/error/{id}")
    @HystrixCommand
    public String paymentInfo_ERROR(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_ERROR(id);
    }

    /**
     * 服务熔断  就是保险丝  服务的降级-> 进而熔断->回复调用链路
     * @param id
     * @return
     */
    @GetMapping(value = "payment/hystrix/circuit/{id}")
    public String paymentCicuitBreaker(@PathVariable("id") Integer id) {
        return paymentService.paymentCicuitBreaker(id);
    }


    // 下面是全局fallback方法
    public String paymentInfoGlobalFallbackMethod(){
        return "Global异常处理信息，请稍后再试";
    }

}
